package databasetest;
//이미선이 더바보~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDesktopPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import java.sql.Connection;
import java.sql.DriverManager;

public class DatabaseTest extends JFrame {
	
	String driver="oracle.jdbc.driver.OracleDriver";
	String url="jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user ="hr";
	String db_pwd="hr";
	Connection conn;
	
	private JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnDb;
	private JMenuItem menuItem;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	


	
	Insert insert;
	Update update;
	Delete delete;
	Select select;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DatabaseTest frame = new DatabaseTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DatabaseTest() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 807, 615);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBackground(SystemColor.inactiveCaptionBorder);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		//데이터베이스 연결 테스트
		//1)드라이버로딩
		try{
			Class.forName(driver).newInstance();
			System.out.println("드라이버가 로딩되었습니다.");
			
		//2)Connection 객체 생성
			conn=DriverManager.getConnection(url, db_user,db_pwd);
			System.out.println("데이터베이스가 정상적으로 연결되었습니다.");	
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnDb());
		}
		return menuBar;
	}
	private JMenu getMnDb() {
		if (mnDb == null) {
			mnDb = new JMenu("DB");
			mnDb.add(getMenuItem());
			mnDb.add(getMntmNewMenuItem());
			mnDb.add(getMntmNewMenuItem_1());
			mnDb.add(getMntmNewMenuItem_2());
		}
		return mnDb;
	}
	private JMenuItem getMenuItem() {
		if (menuItem == null) {
			menuItem = new JMenuItem("입력");
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(insert==null){
						insert= new Insert(conn);
						if(select!=null) insert.setD(select);
						contentPane.add(insert);
					}
					
					insert.setVisible(true);
					insert.toFront();
				
				}
			});
		}
		return menuItem;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("수정");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(update==null){
						update= new Update(conn);
						if(select!=null) {
							select.setD(update);
							update.setU(select);
						}
						
						
						contentPane.add(update);
						
					}
					
					update.setVisible(true);
					update.toFront();
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("삭제");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(delete==null){
						delete= new Delete(conn);
						contentPane.add(delete);
						if(select!=null) delete.setD2(select);
					}
					
					delete.setVisible(true);
					delete.toFront();
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("조회");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(select==null){
						select= new Select(conn);
						if(update!=null) select.setD(update);
						if(insert!=null) insert.setD(select);
						if(delete!=null) delete.setD2(select);
						if(update!=null) update.setU(select);
						contentPane.add(select); 
						
					}
					
					select.setVisible(true);
					select.toFront();
				}
			});
		}
		return mntmNewMenuItem_2;
	}
}
